/**
 * @ author: 汪孟苹
 */
define(["jquery","template","bbcare","oms_config/root"],function($,template,$bb,$root){
	var servers = {};
	/**
	 * 获取url地址,当前用户
	 */
	var URL = $bb.baseUrl("followup");
	var sessinonUserName = JSON.parse($bb.getSessionStorage("account")).alias;
	/**
	 * 请求队列数据
	 */
	servers.ajaxQueueList = function(callback){
		var config = $root.init();
		var showFlag = config.dom.addPatientQueueshowFlag ? "1" : "0"; 
		$bb.ajax({
			url: URL + "followup/service/queue/followQueueListQuery",
			data:JSON.stringify({
				data : { "showFlag":showFlag }
			}),
			success: function(data){
				if(data.success){
					callback(data.data);
				}
			}
		});
	}
	/**
	 * 处理队列数据
	 */
	servers.handleQueues = function(list){
		for(var i = 0; i < list.length; i++){
			list[i].flag = false;
			if( list[i].styleLogo == "" || list[i].styleLogo == null ){
				list[i].styleLogo = "fa fa-tags";
			}
		}
		list[0].flag = true;
		return list;
	}
	/**
	 * 请求建档问卷(随访)
	 */
	servers.ajaxBuildQuestionsFollowup = function(params,callback,values){
		$bb.ajax({
			url: URL + "followup/service/queue/patientAddContentQuery",
			data:JSON.stringify({
				data : {
					"followQueueId": params.queueId,
					"followSchemeId": params.schemeId
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params,values);
				}else{
					$bb.toastShow(data.msg ? data.msg : queueName+"建档问卷添加失败！");
				}
			}
		});
	}
	/**
	 * 请求建档问卷(门诊)
	 */
	servers.ajaxBuildQuestionsClinic = function(params,callback,values,schemeList){
		$bb.ajax({
			url: URL + "followup/service/treat/qryQueueArchiveContent",
			data:JSON.stringify({
				data : {
					"followQueueId": params.queueId,
					"ptSchemeId": params.schemeId
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params,values,schemeList);
				}else{
					$bb.toastShow(data.msg ? data.msg : queueName+"建档问卷添加失败！");
				}
			}
		});
	}
	/**
	 * 请求队列方案(门诊)
	 */
	servers.ajaxQueueScheme = function(params,callback,values){
		$bb.ajax({
			url: URL + "followup/service/treat/listQueueScheme",
			data:JSON.stringify({
				data : {
					"followQueueId": params.queueId
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params,values);
				}else{
					$bb.toastShow(data.msg ? data.msg : queueName+"建档问卷添加失败！");
				}
			}
		});
	}
	/**
	 * 处理获取到的队列方案
	 */
	servers.handleScheme = function(options){
		var arr = [];
		for (var i = 0; i < options.list.length; i++) {
			arr[i] = options.list[i]; 
			arr[i].name = "scheme-" + arr[i].schemeBusiType;
			if( arr[i].schemeBusiType == "0" ){
				arr[i].schemeBusiName = "随访方案";
			}else if(arr[i].schemeBusiType == "1"){
				arr[i].schemeBusiName = "诊疗方案";
			}
			arr[i].val = options.values ? options.values[0].schemeId : arr[i].rows[0].schemeId;
			arr[i].status = arr[i].rows.length == 1 ? "readonly" : options.status;
		}
		var data = {
			"list":arr
		};
		return data;
	}
	/**
	 * 请求保存建档信息(随访)
	 */
	servers.ajaxSaveBuildQuestionsFollowup = function(params,patientName,questiones,callback){
		$bb.ajax({
			url: URL + "followup/service/patient/patientAdd",
			data:JSON.stringify({
				data : {
	            	"followQueueId": params.queueId,
					"opCode": "1000",
					"opNote": "【"+sessinonUserName + "】医生添加患者【" + patientName + "】建档信息，加入队列" + params.queueName,
					"opType": "2",
					"userType": "1",
					"sourceType":"2",
					"questiones": questiones
	            }
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "添加失败！");
				}
			}
		});
	}
	/**
	 * 建档保存时，获取schemeList数据
	 */
	var getDataSchemeList = function(domId){
		var arr = [];
		var selectDom = $("#"+ domId +" .qaCard select");
		var pDom = $("#"+ domId +" .qaCard p.form-control-static");
		for (var i = 0; i < selectDom.length; i++) {
			var obj = {};
			var name = selectDom.eq(i).attr("name");
			obj.schemeId = $("#"+ domId +" .qaCard select[name='" + name + "']").val();
			obj.schemeBusiType = Number(name.replace(/scheme-/, ""));
			arr.push(obj);
		}
		for (var i = 0; i < pDom.length; i++) {
			var obj = {};
			var name = pDom.eq(i).attr("name");
			obj.schemeId = $("#"+ domId +" .qaCard p.form-control-static[name='" + name + "']").attr("value");
			obj.schemeBusiType = Number(name.replace(/scheme-/, ""));
			arr.push(obj);
		}
		return arr;
	}
	/**
	 * 请求保存建档信息(门诊)
	 */
	servers.ajaxSaveBuildQuestionsClinic = function(params,patientName,questiones,callback){
        var schemeList = getDataSchemeList("questionScheme");
		$bb.ajax({
			url: URL + "followup/service/treat/patientAddNew",
			data:JSON.stringify({
				data : {
					"followQueueId": params.queueId,
					"opCode": "1000",
					"opNote": "【"+sessinonUserName + "】医生添加患者【" + patientName + "】建档信息，加入队列" + params.queueName,
					"opType": "2",
					"sourceType":"2",
					"userType": "1",
					"questiones": questiones,
					"schemeList": schemeList
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "添加失败！");
				}
			}
		});
	}
	/**
	 * 根据患者实例id请求患者基本信息(门诊)
	 */
	servers.ajaxPatientInfo = function(params,callback){
		$bb.ajax({
			url: URL + "followup/service/patient/patientInfoQueryByPtId",
			type:"post",
			contentType: "application/json;charset=UTF-8",
			dataType:"json",
			data:JSON.stringify({
				data : {
	            	"patientId": params.patientId,
					"ptSchemeId": params.ptSchemeId
	            }
			}),
			success: function(data){
				if(data.success){
					callback(data.data);
				}else{
					$bb.toastShow(data.msg ? data.msg : "跳转失败！");
				}
			}
		});
	}

	/**
     * 根据页面传递过来的队列id获取当前应展示第i个
     */
    servers.getCurQueue = function(list,followQueueId){
    	for (var i = 0; i < list.length; i++) {
        	if( list[i].id  == followQueueId ){
        		return i;
        	}
        }
    }

	return servers;
});